<template>
  <div>
    <!-- <h2 class="detail-title">实验基本信息</h2>
    <div class="detail-info">
      <div class="detail-item">实验ID：{{data.id}}</div>
      <div class="detail-item">实验名称：{{data.experiment_name}}</div>
      <div class="detail-item">实验时长：{{data.duration}}</div>
      <div class="detail-item">截止时间：{{data.ending_time}}</div>
      <div class="detail-item">知识点列表：{{data.knowledges}}</div>
      <div class="detail-item">关联知识点路径：{{data.konwledges_path}}</div>
    </div> -->

    <h2 class="detail-title">实验环境基本信息</h2>
    <div class="detail-info">
      <div class="detail-item">实验环境ID：{{env.id}}</div>
      <!-- <div class="detail-item">实验名称：{{env.experiment_name}}</div> -->
      <div class="detail-item">镜像名称：{{env.image}}</div>
      <div class="detail-item">子网名称：{{env.subnet_name}}</div>
      <div class="detail-item">子网地址：{{env.cidr_block}}</div>
      <div class="detail-item">是否连接外网：{{env.is_open}}</div>
    </div>
    <h2 class="detail-title">实例列表</h2>
    <TableBar>
      <el-button @click="onMulRec" type="primary" icon="el-icon-plus">批量回收</el-button>
      <el-button @click="onMulSd" icon="el-icon-upload">批量关机</el-button>
    </TableBar>
    <div class="detail-info">
      <el-table :data="tableData"  @selection-change="onSelectionChange" style="width: 100%">
        <el-table-column fixed="left" type="selection" width="55"></el-table-column>
        <el-table-column fixed="left" type="index" width="55">序号</el-table-column>
        <el-table-column prop="id" label="实验环境id"></el-table-column>
        <el-table-column prop="instance_name" label="实例名称"></el-table-column>
        <el-table-column prop="images" label="镜像列表"></el-table-column>
        <el-table-column prop="ip_address" label="ip地址"></el-table-column>
        <el-table-column prop="status" label="实例状态"></el-table-column>
        <el-table-column prop="studentid" label="学号"></el-table-column>
        <el-table-column prop="username" label="用户姓名"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="onRecall(scope.row)"
              size="small"
              class="text-button"
            >回收</el-button>
            <el-button @click="onShutdown(scope.row)" size="small" class="text-error">关机</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <IssueEnv :visible="dialogVisible" :env_id="issue_env_id"  />
    <el-pagination
      background
      layout="prev, pager, next, sizes, jumper"
      @current-change="handleCurrentChange"
      @size-change=handleSizeChange
      :current-page.sync="search.page"
      :page-sizes="page_sizes"
      :page-size="search.page_size"
      :total="total"
    ></el-pagination>

    <el-button class="go-back-btn" @click="goBack" type="primary">返回</el-button>
  </div>
</template>
<script>
import goBack from "@/mixins/goBack";
import pageMixin from '@/mixins/pageMixin.js'
import { experimentModel } from "@/api/admin_models";
import {Message} from 'element-ui';
import TableBar from '@/components/TableBar'
export default {
  mixins: [goBack, pageMixin],
  components: {TableBar},
  data() {
    return {
      data: {},
      id: null,
      env: {},
      total: 0,
      tableData: [],
      search:{
        page: 1,
        page_size: 10
      },
      mulSelectedIns: []
    };
  },
  mounted() {
    this.id = this.$route.params.id;
    this.getItem();
  },
  methods: {
    onSelectionChange(val) {
      this.mulSelectedIns = val;
    },
    onMulSd() {
      if(this.mulSelectedIns.length === 0) {
        return Message.warning('请先选择需要处理实例');
      }
      let ins = this.mulSelectedIns.map(v => {
        return v.id;
      })
      this.doShutdown(ins);
      // this.doRecall(ins);
    },
    onMulRec() {
      if(this.mulSelectedIns.length === 0) {
        return Message.warning('请先选择需要处理实例');
      }
      let ins = this.mulSelectedIns.map(v => {
        return v.id;
      })
      this.doRecall(ins);
      // this.doShutdown(ins);
    },
    doRecall(instances) {
      this.$confirm('确定回收选中实例？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          experimentModel.insBatchRec({
            instances: instances
          }, res => {
            this.$message({
              type: 'success',
              message: res.message 
            });
            this.getEnvs();
          }, err => {console.log(err)})
        }).catch((err) => {
          console.error(err)
          this.$message({
            type: 'info',
            message: '已取消操作'
          });          
        });
    },
    doShutdown(instances) {
      this.$confirm('确定关机选中实例？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          experimentModel.insBatchSd({
            instances: instances
          }, res => {
            this.$message({
              type: 'success',
              message: res.message 
            });
            this.getEnvs();
          }, err => {console.log(err)})
        }).catch((err) => {
          console.error(err)
          this.$message({
            type: 'info',
            message: '已取消操作'
          });          
        });
    },
    onRecall(row) {
      this.doRecall([row.id])
    },
    onShutdown(row) {
      this.doShutdown([row.id])
    },
    getEnvs() {
      // experimentModel.envIns(this.env.experiment_id, res => {
      //   this.tableData = res.data
      // })
    },
    getItem() {
      experimentModel.env(this.id, res => {
        this.env = res.data;
        if(this.env.experiment_id) {
          this.getEnvs()
        }
      });
    }
  }
};
</script>
<style lang="scss">
</style>